package cn.com.wangluotongxin.member.controller;

import cn.com.wangluotongxin.common.exceptions.BadResponseException;
import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.web.FileService;
import cn.hutool.core.map.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@Api(tags = "网络通信-通用 相关接口")
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private FileService fileService;

    @ApiOperation("上传文件")
    @PostMapping("/upload/file")
    public AjaxResult<Map<String, String>> uploadFile(@RequestParam("file") MultipartFile file){
        if(file == null || file.isEmpty()){
            throw new BadResponseException("缺少文件");
        }
        try {
            String filePath = fileService.uploadFile(file);
            return AjaxResult.success(MapUtil.of("url", filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.error("上传失败");
    }
}
